/* Copyright 2009 Requirement Management System
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.object.common;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class ParserFormatterUtils {
	private static ParserFormatterUtils instance;
	private DateParserFormatter datePF = new DateParserFormatter();
	private DecimalParserFormatter decimalPF = new DecimalParserFormatter();
	private IntegerParserFormatter intPF = new IntegerParserFormatter();
	private StringParserFormatter strPF = new StringParserFormatter();
	private BooleanParserFormatter boolPF = new BooleanParserFormatter();
	private String DATE_DEFAULT_PATTERN = "dd/MM/yyyy";
	private String TIME_DEFAULT_PATTERN = "HH:mm";
	private String DATETIME_DEFAULT_PATTERN = "dd/MM/yyyy HH:mm";
	private String DECIMAL_DEFAULT_PATTERN = "#,##0.00";
	private String INTEGER_DEFAULT_PATTERN = "#,##0";
	private Locale locale = Locale.getDefault();
	private String datePattern = DATE_DEFAULT_PATTERN;
	private String decimalPattern = DECIMAL_DEFAULT_PATTERN;
	private String integerPattern = INTEGER_DEFAULT_PATTERN;
	private String timePattern = TIME_DEFAULT_PATTERN;
	private String dateTimePattern = DATETIME_DEFAULT_PATTERN;
	
	public String getDateTimePattern() {
		return dateTimePattern;
	}
	public void setDateTimePattern(String dateTimePattern) {
		this.dateTimePattern = dateTimePattern;
	}
	public String getTimePattern() {
		return timePattern;
	}
	public void setTimePattern(String timePattern) {
		this.timePattern = timePattern;
	}
	public static Date getTimeWithNoDate() {
		return getTimeWithNoDate(new Date());
	}
	public static Date getTimeWithNoDate(Date date) {
		SimpleDateFormat sd = new SimpleDateFormat("H:m:s:S");
		Date newDate = null;
		try {
			newDate = sd.parse(sd.format(date));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return newDate;
	}
	
	public static Date getDateWithNoTime() {
		return getDateWithNoTime(new Date());
	}
	public static Date getDateWithNoTime(Date date) {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		Date newDate = null;
		try {
			newDate = sd.parse(sd.format(date));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return newDate;
	}
	public Boolean parseBoolean(Object value) {
		return boolPF.parse(value);
	}
	public Long parseInteger(String number) {
		return parseInteger(number, null);
	}
	public Long parseInteger(String number, String pattern) {
		return intPF.parse(number, pattern==null?integerPattern:pattern, locale);
	}
	public BigDecimal parseDecimal(String number) {
		Double v= parseDouble(number);
		return v==null?null:new BigDecimal(v.doubleValue());
	}
	public BigDecimal parseDecimal(String number, String pattern) {
		Double v = parseDouble(number, pattern);
		return v==null?null:new BigDecimal(v.doubleValue());
	}
	public Double parseDouble(String number) {
		return parseDouble(number, null);
	}	
	public Double parseDouble(String number, String pattern) {
		return decimalPF.parse(number, pattern==null?decimalPattern:pattern, locale);
	}
	public Date parseDate(String text) {
		return parseDate(text, null);
	}
	public Date parseDate(String text, String pattern) {
		return datePF.parse(text, pattern==null?datePattern:pattern, locale);
	}

	public String object2Text(Object object) {
		return object2Text(object, null);
	}
	public String object2Text(Object object, String pattern) {
		if(object instanceof Number)
			return number2Text((Number)object, pattern);
		else if(object instanceof Date)
			return date2Text((Date)object, pattern);
		else if(object instanceof Boolean)
			return boolean2Text((Boolean)object);
		else if(object instanceof String)
			return (String)object;
		else 
			return (object==null)?null:object.toString();
	}		
	public String number2Text(Number number) {
		return number2Text(number, null);
	}
	public String number2Text(Number number, String pattern) {
		if(number instanceof Long 
		|| number instanceof Integer 
		|| number instanceof Short
		|| number instanceof Byte)
			return integer2Text((Long)number, pattern);
		else if(number instanceof Double)
			return decimal2Text((Double)number, pattern);
		else if(number instanceof BigDecimal)
			return decimal2Text((BigDecimal)number, pattern);
		else
			return null;
	}
	public String boolean2Text(Boolean bool) {
		return boolPF.toText(bool);
	}
	public String integer2Text(Long number) {
		return integer2Text(number, null);
	}			
	public String integer2Text(Long number, String pattern) {
		return intPF.toText(number, pattern==null?integerPattern:pattern, locale);
	}
	public String decimal2Text(Double number) {
		return decimal2Text(number, null);
	}
	public String decimal2Text(BigDecimal number) {
		return decimal2Text(number, null);
	}	
	public String decimal2Text(BigDecimal number, String pattern) {
		return decimalPF.toText(number==null?null:number.doubleValue(), pattern==null?decimalPattern:pattern, locale);
	}	
	public String decimal2Text(Double number, String pattern) {
		return decimalPF.toText(number, pattern==null?decimalPattern:pattern, locale);
	}
	public String date2Text(Date date) {
		return date2Text(date, null);
	}
	public String date2Text(Date date, String pattern) {
		return datePF.toText(date, pattern==null?datePattern:pattern, locale);
	}
	
	public String pattern2Text(String pattern, Object... args) {
		return strPF.toText(pattern, locale, args);
	}
	
	public String getDatePattern() {
		return datePattern;
	}
	public void setDatePattern(String datePattern) {
		this.datePattern = datePattern;
	}
	public String getDecimalPattern() {
		return decimalPattern;
	}
	public void setDecimalPattern(String decimalPattern) {
		this.decimalPattern = decimalPattern;
	}
	public String getIntegerPattern() {
		return integerPattern;
	}
	public void setIntegerPattern(String integerPattern) {
		this.integerPattern = integerPattern;
	}
	public Locale getLocale() {
		return locale;
	}
	public void setLocale(Locale locale) {
		this.locale = locale;
	}
	public static ParserFormatterUtils getInstance() {
		if(instance==null)
			instance = new ParserFormatterUtils();
		return instance;
	}
	public ParserFormatterUtils() {
	}
}
